package www.wxwnb.clerk.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;
import www.wxwnb.clerk.R;
import www.wxwnb.clerk.base.BaseActivity;
import www.wxwnb.clerk.bean.LogBean;
import www.wxwnb.clerk.ui.api.ApiServer;
import www.wxwnb.clerk.ui.api.MyRetrofit;
import www.wxwnb.clerk.utils.Customer;
import www.wxwnb.clerk.utils.UrlConnect;

/**
 * Created by lenovo on 2017/9/8.
 */

public class LogActivity extends BaseActivity implements View.OnClickListener {
    private EditText ed_name_la;
    private EditText ed_pwd_la;
    private Button but_log_la;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.log_activity);
    }
    @Override
    public void initView() {
        ed_name_la = (EditText) findViewById(R.id.ed_name_la);
        ed_pwd_la = (EditText) findViewById(R.id.ed_pwd_la);
        but_log_la = (Button) findViewById(R.id.but_log_la);
        String loginName = Customer.getInstance().getLoginName();
        String password = Customer.getInstance().getPassword();
        Log.i("xxxxxx",loginName+password+"...");
        if(!loginName.equals("")&&!password.equals("")){
            intentActivity(this,MainActivity.class);
            finish();
        }

    }
    @Override
    public void initListener() {
        but_log_la.setOnClickListener(this);
    }


    @Override
    public void initData() {

    }

    @Override
    public void processClick(View v) {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.but_log_la:
                submit();
                break;
        }
    }

    private void submit() {
        String name = ed_name_la.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String pwd = ed_pwd_la.getText().toString().trim();
        if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "用户密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }else {
            //发送网络请求
            sendRequest(name, pwd);
        }

    }

    //发送网络请求
    private void sendRequest(String name, String pwd) {
        showUploadProgressDialog();
        ApiServer apiServer = MyRetrofit.getIntence().getRetrofit(UrlConnect.BASE_URL).create(ApiServer.class);
        Flowable<LogBean> beanFlowable = apiServer.sendLog(name, pwd);
        beanFlowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<LogBean>() {
                    @Override
                    public void onNext(LogBean logBean) {
                      //进行保存用户名和密码
                        if(logBean.getReturnCode()==1){
                            LogBean.DataBean logBeanData = logBean.getData();
                            Customer.getInstance().setLoginName(logBeanData.getLoginName());
                            Customer.getInstance().setPassword(logBeanData.getPassword());
                            Customer.getInstance().setType(logBeanData.getType());
                            Customer.getInstance().setUserId(logBeanData.getId()+"");
                            Customer.getInstance().setUserName(logBeanData.getName());
                            Customer.getInstance().setjobNumber(logBeanData.getJobNumber());
                            Log.i("jobNumber",logBeanData.getJobNumber()+""+logBean.getData().getId());
                            intentActivity(LogActivity.this,MainActivity.class);
                            dismissUploadProgressDialog();
                            finish();
                        }else{
                            dismissUploadProgressDialog();
                            Toast.makeText(LogActivity.this,"没有此用户,请重新登陆",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        dismissUploadProgressDialog();
                        Toast.makeText(LogActivity.this,"网络出现异常，请检查网络",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

}
